package com.csap.framework.oauth2.context;

import com.csap.framework.oauth2.cache.RedisCacheQueryRequest;
import com.csap.framework.oauth2.cache.RedisCacheUpdateRequest;
import com.csap.framework.oauth2.cache.StrategyType;
import com.csap.framework.oauth2.impl.DataFunction;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ycf
 * @Date 2022/3/10 5:25 PM
 * @Version 1.0
 */
@Component
public class CacheApplicationContext implements ApplicationContextAware {
    private static final Map<StrategyType, CacheApplicationData> MAP = new HashMap<>();

    public static CacheApplicationData execute(StrategyType strategyType) {
        return MAP.get(strategyType);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansOfType(RedisCacheQueryRequest.class).forEach((k, v) -> MAP.put(v.strategyType(), CacheApplicationData.builder().queryRequest(v).build()));
        applicationContext.getBeansOfType(RedisCacheUpdateRequest.class).forEach((k, v) -> MAP.get(v.strategyType()).setUpdateRequest(v));
        applicationContext.getBeansOfType(DataFunction.class).forEach((k, v) -> MAP.get(v.strategyType()).setData(v));
    }
}
